/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-09-02
 * Time: 20:10
 */

/**
 * 题目名称：
 * 使用两个线程来累加 count 的值
 * 题目内容：
 * 每个线程循环 1w 次，累加变量 count 的值，count 默认值为 0，注意线程安全问题。
 */
public class demo1 {
    public static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (locker) {
                    count++;
                }
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (locker) {
                    count++;
                }
            }
        });
        t1.start();
        t2.start();
        Thread.sleep(1000);
        System.out.println(count);
    }
}
